package com.jeffse.light.stream.core.data.backend;

import com.jeffse.light.stream.core.data.descriptor.stateDescriptor.StateDescriptor;
import com.jeffse.light.stream.core.utils.CacheUtils;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

/**
 * 本地内存状态
 */
public class LocalBackends implements StateBackends {

    @Override
    public void update(StateDescriptor stateDescriptor, Object o) {
        Cache cache = CacheUtils.getINSTANCE().getCache(stateDescriptor.getNameSpace());
        Element element2 = new Element(stateDescriptor.getStateKey(), o);
        cache.put(element2);
    }

    @Override
    public void clear(StateDescriptor stateDescriptor) {
        Cache cache = CacheUtils.getINSTANCE().getCache(stateDescriptor.getNameSpace());
        cache.remove(stateDescriptor.getStateKey());
    }

    @Override
    public Element getState(StateDescriptor stateDescriptor) {
        Cache cache = CacheUtils.getINSTANCE().getCache(stateDescriptor.getNameSpace());
        Element element = cache.get(stateDescriptor.getStateKey());
        return element;
    }

}
